<template>
	<view class="comment">
		<uni-header :title="'评语模板'"></uni-header>
		<view class="comment-body">
			<view class="comment-card" v-for="item in commentData">
				<view class="card-content">{{item.comment}}</view>
				<view class="card-footer">
					<view class="icon edit" @click="inputDialogToggle(item)"><uni-icons type="compose" color="#3c96f3"
							size="20"></uni-icons>修改</view>
					<view class="icon delete" @click="deleteClick(item)"><uni-icons type="trash" color="#f33c76"
							size="20"></uni-icons>删除</view>
				</view>
			</view>
			<view class="icon add" @click="inputDialogToggle">
				<u-icon name="plus-circle-fill" color="#2979ff" size="50"></u-icon>
			</view>
		</view>
		<uni-popup ref="inputDialog" type="dialog">
			<uni-popup-dialog ref="inputClose" mode="input" title="请输入评语内容" :value="comment" placeholder="请输入内容"
				@confirm="addCommentData"></uni-popup-dialog>
		</uni-popup>

		<uni-popup ref="alertDialog" type="dialog">
			<uni-popup-dialog :type="msgType" cancelText="取消" confirmText="确定" title="通知" content="确定要删除该模板吗？？"
				@confirm="dialogConfirm"></uni-popup-dialog>
		</uni-popup>
	</view>
</template>

<script>
	import {
		addComment,
		getComment,
		editComment,
		deleteComment
	} from "@/api/mine"
	export default {
		data() {
			return {
				type: 'center',
				msgType: 'error',
				id: "",
				comment: "",
				commentData: []
			}
		},
		created() {
			this.getCommentData();
		},
		methods: {
			inputDialogToggle(data) {
				if (data) {
					this.comment = data.comment
					this.id = data.id
				}
				this.$refs.inputDialog.open()
			},
			//添加修改评语
			addCommentData(val) {
				if (this.id) {
					editComment({
						comment: val,
						deviceId: this.$store.state.user.deviceId,
						id: this.id
					}).then(res => {
						if (res.status == 0) {
							uni.$u.toast('修改成功');
							this.getCommentData();
						}
					})
				} else {
					addComment({
						comment: val,
						deviceId: this.$store.state.user.deviceId
					}).then(res => {
						if (res.status == 0) {
							uni.$u.toast('添加成功');
							this.getCommentData();
						}
					})
				}
			},
			//获取评语
			getCommentData() {
				getComment({
					deviceId: this.$store.state.user.deviceId
				}).then(res => {
					this.commentData = res.dataList.list
				})
			},
			//删除评语
			deleteClick(data) {
				this.id = data.id;
				this.$refs.alertDialog.open();
			},
			dialogConfirm() {
				deleteComment({
					id: this.id,
					deviceId: this.$store.state.user.deviceId
				}).then(res => {
					if (res.status == 0) {
						uni.$u.toast('删除成功');
						this.getCommentData();
					}
				})
			},
		}
	}
</script>

<style lang="scss">
	.comment-body {
		padding: 15px;

		.comment-card {
			border: 1px solid #eee;
			padding: 10px;
			border-radius: 10px;
			background: #fff;
			margin-bottom: 10px;

			.card-content {
				overflow: hidden;
				text-overflow: ellipsis;
				display: -webkit-box;
				-webkit-line-clamp: 3;
				-webkit-box-orient: vertical;
			}

			.card-footer {
				display: flex;
				align-items: center;
				border-top: 1px solid #eee;
				padding-top: 10px;
				margin-top: 10px;

				.icon {
					display: flex;
					align-items: center;
					margin-right: 15px;
				}

				.edit {
					color: #3c96f3;
				}

				.delete {
					color: #f33c76;
				}

			}
		}

		.add {
			position: fixed;
			bottom: 10vh;
			right: 5vw;
		}
	}
</style>